package me.sealer.ssm.config;

import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 * 功能描述： $ ExceptionConfig
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年12月17日 17时23分37秒
 */
@Configuration
public class ExceptionConfig {
    @Bean
    public SimpleMappingExceptionResolver exceptionResolver() {
        SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
        Properties properties = new Properties();
        properties.setProperty("org.springframework.web.multipart.MaxUploadSizeExceededException", "fileUploadError");
        exceptionResolver.setExceptionMappings(properties);
        return exceptionResolver;
    }

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return factory -> {
            //状态码 ：HttpStatus.NOT_FOUND（404）
            ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/error404");
            ErrorPage errorPage405 = new ErrorPage(HttpStatus.FORBIDDEN, "/error/error405");
            ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/error500");

            Set<ErrorPage> errorPageSet = new HashSet<>(8);
            errorPageSet.add(errorPage404);
            errorPageSet.add(errorPage405);
            errorPageSet.add(errorPage500);
            factory.setErrorPages(errorPageSet);
        };
    }
}
